package com.orangelife.service.propFeeCommInfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeBuilding.IPropFeeBuildingService;
import com.orangelife.service.propFeeBuilding.model.PropFeeBuildingDto;
import com.orangelife.setting.constant.TypeConst;

public class HandleBuildingThread extends Thread implements Runnable {

	private String threadName;
	private List<Map<String, Object>> data;

	private int start;
	private int end;

	private IPropFeeBuildingService propFeeBuildingService;

	public HandleBuildingThread(String threadName,
			List<Map<String, Object>> data,
			IPropFeeBuildingService propFeeBuildingService) {
		this.threadName = threadName;
		this.data = data;
		this.propFeeBuildingService = propFeeBuildingService;
	}

	public void run() {
		try {

			/***
			 * 批量查询楼栋
			 */
			List<String> buildingIDs = new ArrayList<String>();
			List<PropFeeBuildingDto> feeBuildings = new ArrayList<PropFeeBuildingDto>();
			for (Map<String, Object> building : data) {
				if (!StringUtils.isBlank(building.get("buildingID").toString())) {
					buildingIDs.add(building.get("buildingID").toString());
				}

			}

			if (buildingIDs != null && buildingIDs.size() > 0) {
				feeBuildings = propFeeBuildingService
						.bauthfindPropFeeBuilding(buildingIDs);
			}
			List<PropFeeBuildingDto> propFeeBuildings = new ArrayList<PropFeeBuildingDto>();
			for (Map<String, Object> building : data) {
				System.out.println(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t"
						+ JSONObject.fromObject(building).toString());

				/***
				 * 判断楼栋是否存在
				 */
				if (!StringUtils.isBlank(building.get("buildingID").toString())) {
					PropFeeBuildingDto feeBuilding = null;
					for (PropFeeBuildingDto propFeeBuildingDto : feeBuildings) {
						if (propFeeBuildingDto.getBuildingID().equals(
								building.get("buildingID").toString())) {
							feeBuilding = propFeeBuildingDto;
							break;
						}
					}
					/***
					 * 存在更新
					 */
					if (feeBuilding != null) {
						feeBuilding.setBuildingNameOther(building
								.get("buildingNameOther") != null ? building
								.get("buildingNameOther").toString() : null);
						feeBuilding.setBuildingSort(building
								.get("buildingSort") != null ? Integer
								.parseInt(building.get("buildingSort")
										.toString()) : null);
						feeBuilding.setBuildingName(building
								.get("buildingName").toString());
						//feeBuilding.setTsedit(new Date());
						propFeeBuildingService.editPropFeeBuilding(feeBuilding);

					} else {

						/***
						 * 不存在添加
						 */
						List<PropFeeBuildingDto> buildings = propFeeBuildingService
								.findPropFeeBuildingByRelevantID(null, building
										.get("commID").toString(), null, null,
										building.get("buildingName").toString());
						if (buildings == null || buildings.size() <= 0) {
							PropFeeBuildingDto propFeeBuilding = new PropFeeBuildingDto();
							propFeeBuilding.setBuildingID(building.get(
									"buildingID").toString());

							propFeeBuilding.setCommID(building.get("commID")
									.toString());

							propFeeBuilding
									.setBuildingNameOther(building
											.get("buildingNameOther") != null ? building
											.get("buildingNameOther")
											.toString() : null);
							propFeeBuilding.setBuildingSort(building
									.get("buildingSort") != null ? Integer
									.parseInt(building.get("buildingSort")
											.toString()) : null);

							propFeeBuilding.setBuildingName(building.get(
									"buildingName").toString());

							propFeeBuilding.setItemStat(TypeConst.FLAG_ON);
							propFeeBuilding.setTscreate(new Date());
							propFeeBuilding.setTsedit(new Date());
							propFeeBuildings.add(propFeeBuilding);

						}

					}

				}

			}
			propFeeBuildingService.addPropFeeBuildings(propFeeBuildings);

		} catch (AppBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
